package com.dykj.fireFighting.server.socialUnit.devHis.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dykj.fireFighting.api.socialUnit.devHis.dto.AlarmRecordDto;
import com.dykj.fireFighting.api.socialUnit.devHis.dto.DevHisDto;
import com.dykj.fireFighting.api.socialUnit.devHis.entity.AlarmRecord;
import com.dykj.fireFighting.api.socialUnit.devHis.entity.DevHis;
import com.dykj.fireFighting.api.socialUnit.devHis.service.IDevHisService;
import com.dykj.fireFighting.api.socialUnit.devHis.vo.DevHisVo;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;

import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.common.starter.datasource.configure.RequestDataHelper;
import com.dykj.fireFighting.server.socialUnit.devHis.mapper.DevHisMapper;
import com.dykj.fireFighting.server.socialUnit.devicemanger.mapper.DeviceMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.*;

/**
 * 火灾自动报警设备运行记录
 *
 * @Author hxb
 * @Date 2021-06-30 14:45:09
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class DevHisServiceImpl extends BaseServiceImpl<DevHisMapper, DevHis> implements IDevHisService {


    @Autowired
    private DevHisMapper devHisMapper;
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public PageVo<DevHis> queryList(DevHisDto devHisDto, String tableName) {

        Map<String, Object> params = new HashMap<>();
        params.put("status", devHisDto.getStatus());
        if (null != (devHisDto.getDevCode()) && StringUtils.isNotBlank(devHisDto.getDevCode())) {

            params.put("devCode", "%" + devHisDto.getDevCode() + "%");
        }
        params.put("unitId", devHisDto.getUnitId());
        params.put("tableName", tableName);

        Page page = new Page();
        // 设置当前页码
        page.setCurrent(devHisDto.getPage());
        // 设置页大小
        page.setSize(devHisDto.getLimit());
        return new PageVo<DevHis>(this.baseMapper.queryList(page, params));
    }

    @Override
    public PageVo<AlarmRecord> queryAlarmList(AlarmRecordDto alarmRecordDto) {
        Page page = new Page();
        // 设置当前页码
        page.setCurrent(alarmRecordDto.getPage());
        // 设置页大小
        page.setSize(alarmRecordDto.getLimit());
        return new PageVo<AlarmRecord>(devHisMapper.queryAlarmList(page, alarmRecordDto));
    }

    @Override
    public PageVo<DevHisVo> queryDevHisByRelationId(String relationId, long pageNum, long limit) {
        Page page = new Page();
        // 设置当前页码
        page.setCurrent(pageNum);
        // 设置页大小
        page.setSize(limit);
        Map<String, String> params = new HashMap<>();
        params.put("relationId", relationId);
        String tableName = deviceMapper.getTableName(relationId);
        if (StringUtils.isBlank(tableName)) {
            return new PageVo<DevHisVo>();
        }
        params.put("tableName", tableName);
        return new PageVo<DevHisVo>(devHisMapper.queryDevHisByRelationId(page, params));
    }

    @Override
    public void updateAndInsertImg(DevHis devHis) throws Exception {
        devHisMapper.updateById(devHis);
    }

    @Override
    public int getFireNum(String s) {
        return this.baseMapper.getFireNum(s, null, null);
    }

    @Override
    public int getWait(String s) {
        return this.baseMapper.getWait(s);
    }

    @Override
    public int getFireRateScore(String start, String end, String unitId) {
        int totalDevice = deviceMapper.getNumByUnitId(unitId);
        int fireNum = devHisMapper.getFireNum(unitId, start, end);
        //开始计算了
        if (totalDevice != 0) {
            double a = (double) fireNum / (double) totalDevice;
            if (a <= 0.05) {
                return 10;
            }
            if (a > 0.05 && a <= 0.2) {
                return 5;
            }
            if (a > 0.2) {
                return 0;
            }

        } else {
            return 10;
        }
        return 0;

    }

    @Override
    public int getFireGZRateScore(String start, String end, String unitId) {
        int totalDevice = deviceMapper.getNumByUnitId(unitId);
        int fireNum = devHisMapper.getFireGZNum(unitId, start, end);
        //开始计算了
        if (totalDevice != 0) {
            double a = (double) fireNum / (double) totalDevice;
            if (a <= 0.05) {
                return 10;
            }
            if (a > 0.05 && a <= 0.2) {
                return 5;
            }
            if (a > 0.2) {
                return 0;
            }

        } else {
            return 10;
        }
        return 0;
    }

    @Override
    public int getFaultNotReadNum(String unitId) {
        return this.baseMapper.getFaultNotReadNum(unitId);
    }

    @Override
    @Transactional
    public PageVo<DevHisVo> getFaultList(long pageNum, long limit, Map<String, String> params) {

        Page page = new Page();
        // 设置当前页码
        page.setCurrent(pageNum);
        // 设置页大小
        page.setSize(limit);
        IPage faultList = this.baseMapper.getFaultList(page, params);
        if ("0".equals(params.get("readType"))) {
            this.baseMapper.updateFaultReadType(params.get("unitId"));
        }
        return new PageVo<DevHisVo>(faultList);
    }

    @Override
    @Transactional
    public PageVo<DevHisVo> getOtherList(long pageNum, long limit, String unitId) {
        Page page = new Page();
        // 设置当前页码
        page.setCurrent(pageNum);
        // 设置页大小
        page.setSize(limit);
        return new PageVo<DevHisVo>(this.baseMapper.getDevHisOther(page, unitId));
    }

    @Override
    public DevHis getDevHisById(String id) {
        Map<String, Object> patams = new HashMap();
        patams.put("id", id);
        RequestDataHelper.setRequestData(patams);
        return this.baseMapper.selectById(id);
    }

    @Override
    public int isExistTable(String tableName) {
        return this.baseMapper.isExistTable(tableName);
    }

    @Override
    public int updateDevHis(DevHis devHis) {
        Map<String, Object> patams = new HashMap();
        patams.put("id", devHis.getId());
        patams.put("isRead",devHis.getIsRead());
        RequestDataHelper.setRequestData(patams);
        this.baseMapper.updateById(devHis);
        return 1;
    }




    @Override
    public DevHis queryByRelationId(String relationId) {
        return this.baseMapper.queryByRelationId(relationId);
    }
}
